from PyQt5.QtWidgets import *
import sys
from testplot import Ui_Dialog#  改动

import matplotlib
matplotlib.use("Qt5Agg")  # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus'] = False
data = [[(0, 0), (0, 60), (64, 60), (64, 0)]]    #轮廓平行的坐标用x,y表示
new_data = []
Qi_x_list = []
Qi_y_list = []
sd = -5   #安全距离
#直线x坐标的列表生成
#           n = int((x[n] - x[0])/(-sd))
#           for  i <= n ,i++   (n为内框之差除以-sd的向前取整最大值)
#           x_1[i+1] = x_1[i] - sd
#           [[(x_1[i],y[0], (x_1[i], y[n])]]
data_0 = [[(10, 10), (10, 50)]]          #方向平行路径坐标使用m,n表示
data_1 = [[(15, 10), (15, 50)]]
data_2 = [[(20, 10), (20, 50)]]
data_3 = [[(25, 10), (25, 50)]]
data_4 = [[(30, 10), (30, 50)]]
data_5 = [[(35, 10), (35, 50)]]
data_6 = [[(40, 10), (40, 50)]]
data_7 = [[(45, 10), (45, 50)]]
data_8 = [[(50, 10), (50, 50)]]
data_9 = [[(55, 10), (55, 50)]]
#data_10 = [[(60, 10), (60, 50)]]

#创建一个matplotlib图形绘制类
class MyFigure(FigureCanvas):
    def __init__(self, width=5, height=4, dpi=100):
        #第一步：创建一个创建Figure
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        #第二步：在父类中激活Figure窗口
        super(MyFigure, self).__init__(self.fig) #此句必不可少，否则不能显示图形
        #第三步：创建一个子图，用于绘制图形用，111表示子图编号，如matlab的subplot(1,1,1)
        self.patch = self.fig.add_subplot(111)
        #第四步：就是画图，【可以在此类中画，也可以在其它类中画】


class MainDialogImgBW(QDialog, Ui_Dialog):
    def __init__(self):
        super(MainDialogImgBW, self).__init__()
        self.setupUi(self)
        self.setWindowTitle("田块显示")
        #self.setMinimumSize(0,0)

        #第五步：定义MyFigure类的一个实例
        self.F = MyFigure(width=3, height=2, dpi=100)
        #self.F.plotsin()
        # self.rectangle()

        for i in range(len(data)):
            x, y = self.rectangle(data[i])
            x.append(x[0])  # 增加第一个坐标使得矩形封闭
            y.append(y[0])  # 五个坐标点使得图形封闭
            self.F.patch.plot(x, y, c='k')

        x.append(x[1])
        y.append(y[1])
        # 求两条邻边的向量
        for j0 in range(len(data_0)):
            x0, y0 = self.line0(data_0[j0])
            self.F.patch.plot(x0, y0, c='k')
        for j1 in range(len(data_1)):
            x1, y1 = self.line1(data_1[j1])
            self.F.patch.plot(x1, y1, c='k')
        for j2 in range(len(data_2)):
            x2, y2 = self.line2(data_2[j2])
            self.F.patch.plot(x2, y2, c='k')
        for j3 in range(len(data_3)):
            x3, y3 = self.line3(data_3[j3])
            self.F.patch.plot(x3, y3, c='k')
        for j4 in range(len(data_4)):
            x4, y4 = self.line4(data_4[j4])
            self.F.patch.plot(x4, y4, c='k')
        for j5 in range(len(data_5)):
            x5, y5 = self.line5(data_5[j5])
            self.F.patch.plot(x5, y5, c='k')
        for j6 in range(len(data_6)):
            x6, y6 = self.line6(data_6[j6])
            self.F.patch.plot(x6, y6, c='k')
        for j7 in range(len(data_7)):
            x7, y7 = self.line7(data_7[j7])
            self.F.patch.plot(x7, y7, c='k')
        for j8 in range(len(data_8)):
            x8, y8 = self.line8(data_8[j8])
            self.F.patch.plot(x8, y8, c='k')
        for j9 in range(len(data_9)):
            x9, y9 = self.line0(data_9[j9])
            self.F.patch.plot(x9, y9, c='k')
        # for j10 in range(len(data_10)):
        #     x10, y10 = self.line10(data_10[j10])
        #     self.F.patch.plot(x10, y10, c='k')
        for i in range(len(x)):
            if i < (len(x) - 2):
                # 求边的长度
                d1 = ((x[i + 1] - x[i]) ** 2 + (y[i + 1] - y[i]) ** 2) ** 0.5  # **2  代表平方
                d2 = ((x[i + 1] - x[i + 2]) ** 2 + (y[i + 1] - y[i + 2]) ** 2) ** 0.5
                # 两条边的夹角
                # 内积ab,sinA
                # ab=(x[i+1]-x[i])*(y[i+1]-y[i])+(x[i+1]-x[i+2])*(y[i+1]-y[i+2])
                ab = (x[i + 1] - x[i]) * (x[i + 1] - x[i + 2]) + (y[i + 1] - y[i]) * (y[i + 1] - y[i + 2])
                cosA = ab / (d1 * d2)
                if cosA == 0:  #ab内积为零   两直线垂直
                    sinA = 1
                    # 向量V1,V2的坐标
                    dv1 = sd / sinA  # V1,V2长度相等
                    v1_x = (dv1 / d1) * (x[i + 1] - x[i])
                    v1_y = (dv1 / d1) * (y[i + 1] - y[i])
                    v2_x = (dv1 / d2) * (x[i + 1] - x[i + 2])
                    v2_y = (dv1 / d2) * (y[i + 1] - y[i + 2])
                    PiQi_x = v1_x + v2_x
                    PiQi_y = v1_y + v2_y
                    Qi_x = PiQi_x + x[i + 1]
                    Qi_x_list.append(Qi_x)
                    Qi_y = PiQi_y + y[i + 1]
                    Qi_y_list.append(Qi_y)
        Qi_x_list.append(Qi_x_list[0])
        Qi_y_list.append(Qi_y_list[0])

        self.F.patch.plot(Qi_x_list, Qi_y_list, c='k')
        #第六步：在GUI的groupBox中创建一个布局，用于添加MyFigure类的实例（即图形）后其他部件。
        self.gridlayout = QGridLayout(self.groupBox)  # 继承容器groupBox
        self.gridlayout.addWidget(self.F, 0, 1)

    def rectangle(self, data):  # 输入参数data  数据列表
        x = []
        for i in range(len(data)):  # 返回x列表中的元素
            x.append(data[i][0])
        y = []
        for i in range(len(data)):
            y.append(data[i][1])
        return x, y  # 输出参数 x，y组成的坐标集合

    def line0(self, data_0):
        x0 = []
        for j0 in range(len(data_0)):
            x0.append(data_0[j0][0])
        y0 = []
        for j0 in range(len(data_0)):
            y0.append(data_0[j0][1])
        return x0, y0

    def line1(self, data_1):
        x1 = []
        for j1 in range(len(data_1)):
            x1.append(data_1[j1][0])
        y1 = []
        for j1 in range(len(data_1)):
            y1.append(data_1[j1][1])
        return x1, y1

    def line2(self, data_2):
        x2 = []
        for j2 in range(len(data_2)):
            x2.append(data_2[j2][0])
        y2 = []
        for j2 in range(len(data_2)):
            y2.append(data_2[j2][1])
        return x2, y2

    def line3(self, data_3):
        x3 = []
        for j3 in range(len(data_3)):
            x3.append(data_3[j3][0])
        y3 = []
        for j3 in range(len(data_3)):
            y3.append(data_3[j3][1])
        return x3, y3

    def line4(self, data_4):
        x4 = []
        for j4 in range(len(data_4)):
            x4.append(data_4[j4][0])
        y4 = []
        for j4 in range(len(data_4)):
            y4.append(data_4[j4][1])
        return x4, y4

    def line5(self, data_5):
        x5 = []
        for j5 in range(len(data_5)):
            x5.append(data_5[j5][0])
        y5 = []
        for j5 in range(len(data_5)):
            y5.append(data_5[j5][1])
        return x5, y5

    def line6(self, data_6):
        x6 = []
        for j6 in range(len(data_6)):
            x6.append(data_6[j6][0])
        y6 = []
        for j6 in range(len(data_6)):
            y6.append(data_6[j6][1])
        return x6, y6

    def line7(self, data_7):
        x7 = []
        for j7 in range(len(data_7)):
            x7.append(data_7[j7][0])
        y7 = []
        for j7 in range(len(data_7)):
            y7.append(data_7[j7][1])
        return x7, y7

    def line8(self, data_8):
        x8 = []
        for j8 in range(len(data_8)):
            x8.append(data_8[j8][0])
        y8 = []
        for j8 in range(len(data_8)):
            y8.append(data_8[j8][1])
        return x8, y8

    def line9(self, data_9):
        x9 = []
        for j9 in range(len(data_9)):
            x9.append(data_9[9][0])
        y9 = []
        for j9 in range(len(data_9)):
            y9.append(data_9[j9][1])
        return x9, y9

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = MainDialogImgBW()
    main.show()
    #app.installEventFilter(main)
    sys.exit(app.exec_())